package com.wlient.springboot_druiddatasource.component.datasource.entity;

/**
 * @author: 史炜立
 * @description: TODO
 * @date: 2022/11/21 13:54
 */
import java.sql.*;
public class JDBCUpdateByStatement{
    final static String DRIVER = "com.mysql.cj.jdbc.Driver";
    // 数据库的实例名是 shiyan
    final static String URL = "jdbc:mysql://1.117.89.11:3306/wlient?useUnicode=true&characterEncoding=utf8";
    final static String USERNAME = "root";
    final static String PASSWORD = "wlient990128.";
    static Connection connection = null;
    static Statement stmt = null;
    static ResultSet rs = null;
    public static void executeQuery() {
        try{
            // 1 加载数据库驱动
            Class.forName(DRIVER);
            // 2 获取数据库连接
            connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
            // 3 通过连接，获取一个操作数据库 Statement 的对象
            stmt = connection.createStatement();
            // 4 通过 executeQuery()实现对数据库的查询，并返回一个结果集（结果集中包含了所有查询到的数据）
            String querySql = "SELECT * FROM vote_record limit 1";
            rs = stmt.executeQuery(querySql);
            // 5 通过循环读取结果集中的数据
            while(rs.next()) {
                ResultSetMetaData metaData = rs.getMetaData();
                System.out.println(metaData);
                int columnCount = metaData.getColumnCount();
                System.out.println(columnCount);
//                System.out.println(rs.getType());

            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            try {
                // 注意 rs、stmt、connection 三个对象的关闭顺序
                if(rs != null)rs.close();
                if(stmt != null)stmt.close();
                if(connection != null)connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args){
        executeQuery();
    }


}
